package com.example.demo_mysql_mybatisplus.util.controller.advicer.core;

import cn.hutool.core.util.StrUtil;
import lombok.Cleanup;
import lombok.Getter;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * rest异常
 * 不对外开放，只在本包内使用
 */
@Getter
public class RestException extends RuntimeException {

    private Integer code;
    private String desc;

    public RestException(Integer code, String desc) {
        super( desc );
        this.code = code;
        this.desc = desc;
    }


    /** 获取异常堆栈信息 */
    public static String getStackTrace(Throwable throwable) {
        StringWriter sw = new StringWriter();
        @Cleanup PrintWriter pw = new PrintWriter(sw);
        throwable.printStackTrace(pw);
        String errorResult  = StrUtil.subPre(sw.toString(),3000) ;
        return errorResult;
    }
}
